package com.wutong.eams.controller;

import com.wutong.eams.enums.Role;
import com.wutong.eams.utils.ObjectUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/api/dashboard/*")
public class DashboardController extends BaseController {
    @Override
    public Role getPermission() {
        return Role.ADMIN;
    }

    @Override
    public boolean needPermission() {
        return true;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String pathInfo = req.getPathInfo();
        if (pathInfo == null) pathInfo = "/";

        switch (pathInfo) {
            case "/summary":
                getSummaryData(req, resp);
                break;
            default:
                resp.sendError(404);
        }
    }

    private void getSummaryData(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        // 从数据库获取统计数据
        Map<String, Object> data = new HashMap<>();
        data.put("studentCount", getStudentManagerService().getAllStudentInfos().size());
        data.put("courseCount", 2);
        data.put("todoCount", 3);
        data.put("messageCount", 4);
        data.put("userCount", getUserManagerService().countUsers());

        resp.setContentType("application/json");
        resp.getWriter().write(ObjectUtil.toJson(data));
    }

}
